package com.project.service.impl;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class ServiceProxyFactory implements InvocationHandler {
    private Object targetObj;
    private static SqlSessionFactory factory;
    static {
        try {
            Reader r = Resources.getResourceAsReader("mybatis.cfg.xml");
            factory = new SqlSessionFactoryBuilder().build(r);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public ServiceProxyFactory(Object targetObj){
        this.targetObj=targetObj;
    }
    public Object getProxy(){
        return Proxy.newProxyInstance(
                targetObj.getClass().getClassLoader(),
                targetObj.getClass().getInterfaces(),
                this
        );
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        SqlSession session = factory.openSession();
        //得到目标对象书写列表
        Field[] fieldArray = targetObj.getClass().getDeclaredFields();
        for (Field f:fieldArray){
            //判断属性是否为接口
            if (f.getType().isInterface()){
                //如果是 就产生接口实现类对象
                Object mapper = session.getMapper(f.getType());
                //打破访问修饰符的访问
                f.setAccessible(true);
                //给mapper属性赋值
                f.set(targetObj,mapper);
            }
        }
        Object returnObj = null;
        try{
            returnObj = method.invoke(targetObj,args);
            session.commit();
        }catch (Exception e){
            e.printStackTrace();
            session.rollback();
        }finally {
            session.close();
        }
        return returnObj;
    }
}
